﻿using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
using WebApplication1.Models;

namespace WebApplication1.Controllers
{
    public class LoginController : Controller
    {
        /// <summary>
        /// 登录方法
        /// </summary>
        [HttpPost]
        public async Task<IActionResult> Login(LoginModel loginModel)
        {
            // 登录成功
            if (loginModel.userName == "admin")
            {
                //1、创建身份 ClaimsIndentity
                //1.1、 创建声明。用户的描述 用户名 密码 角色
                // 总结：1、声明类型： 给声明设置规范
                //       2、声明类型标准：给声明明确统一的规范
                Claim claim = new Claim(ClaimTypes.Name, loginModel.userName);
                Claim claim2 = new Claim("password", loginModel.password);

                //1.2 创建声明集合
                var claims = new[] { claim, claim2 };

                //1.3 创建身份
                ClaimsIdentity claimsIdentity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);

                // 2、实现登录
                //
                await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(claimsIdentity));

                // 3、返回
                return Ok("登录成功");
            }
            else
            {
                return Ok("用户名或者密码不正确");
            }

        }

        /// <summary>
        /// 身份认证
        /// </summary>
        /// <returns></returns>
        [HttpPost("GetUser")]
        public async Task<IActionResult> GetUser()
        {
            // 1、获取用户信息
            string name = HttpContext.User.Identity.Name;
            string password = HttpContext.User.Claims.FirstOrDefault(t => t.Type == "password").Value;

            return Ok($"Name:{name}  Password:{password}");
        }

        /// <summary>
        /// 用户注销
        /// </summary>
        /// <returns></returns>
        [HttpPost("LogOut")]
        public async Task<IActionResult> LogOut()
        {
            // 1、用户注销
            await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
            return Ok("注销成功");
        }

        /// <summary>
        /// 登录身份有效期
        /// </summary>
        [HttpPost("LoginTime")]
        public async Task<IActionResult> LoginTime(LoginModel loginModel)
        {
            // 登录成功
            if (loginModel.userName == "admin")
            {
                //1、创建身份 ClaimsIndentity
                //1.1、 创建声明。用户的描述 用户名 密码 角色
                // 总结：1、声明类型： 给声明设置规范
                //       2、声明类型标准：给声明明确统一的规范
                Claim claim = new Claim(ClaimTypes.Name, loginModel.userName);
                Claim claim2 = new Claim("password", loginModel.password);

                //1.2 创建声明集合
                var claims = new[] { claim, claim2 };

                //1.3 创建身份
                ClaimsIdentity claimsIdentity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);

                // 2、实现登录
                AuthenticationProperties authenticationProperties = new AuthenticationProperties();
                authenticationProperties.IsPersistent = true; // 是否持久化
                authenticationProperties.ExpiresUtc = DateTimeOffset.UtcNow.AddMinutes(1); //持续一分钟
                await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme,
                    new ClaimsPrincipal(claimsIdentity), authenticationProperties);

                // 3、返回
                return Ok("登录成功");
            }
            else
            {
                return Ok("用户名或者密码不正确");
            }

        }
    }
}
